package com.example.joke.frame

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import com.example.joke.R
import com.example.joke.base.navigationbar.AbsNavigationBar

class DefaultNavigationBar(params: Builder.DefaultNavigationParams)
    : AbsNavigationBar<DefaultNavigationBar.Builder.DefaultNavigationParams>(params) {
    override fun bindLayoutId(): Int {
        return R.layout.title_bar
    }

    override fun applyView() {
        // 绑定效果
        if (!getParams().mTitle.isNullOrEmpty()) {
            setText(R.id.tv_title, getParams().mTitle!!)
        }
        if (!getParams().mRightText.isNullOrEmpty()) {
            setText(R.id.tv_right, getParams().mRightText!!)
        }
        if (getParams().mRrightIcon != 0) {
            setIcon(R.id.iv_right, getParams().mRrightIcon)
        }
        // TODO: 可以扩展一下显示隐藏效果

        // 左边默认关闭，写死即可，如果不是关闭就在上层传一个点击事件过来
        setOnClickListener(R.id.iv_back, getParams().mLeftClickListener)

        if (getParams().mRightClickListener != null) {
            setOnClickListener(R.id.tv_right, getParams().mRightClickListener!!)
        }
    }

    class Builder(context: Context, parent: ViewGroup?) : AbsNavigationBar.Builder(context, parent) {

        var P: DefaultNavigationParams
        init {
            P = DefaultNavigationParams(context, parent)
        }

        constructor(context: Context) : this(context, null) {

        }
        open override fun <P : AbsNavigationParams> builder(): AbsNavigationBar<P> {
            val navigationBar: DefaultNavigationBar = DefaultNavigationBar(P)
            return navigationBar as AbsNavigationBar<P>
        }


        // 1. 设置所有效果
        public fun setTitle(title: String): Builder {
            P.mTitle = title
            return this
        }

        public fun setRightText(rightText: String): Builder {
            P.mRightText = rightText
            return this
        }

        public fun setRightIcon(rightIcon: Int): Builder {
            P.mRrightIcon = rightIcon
            return this
        }

        // TODO: 可以扩展一下显示隐藏效果
        public fun setLeftClickListener(listener: OnClickListener): Builder {
            P.mLeftClickListener = listener
            return this
        }

        public fun setRightClickListener(listener: OnClickListener): Builder {
            P.mRightClickListener = listener
            return this
        }

        class DefaultNavigationParams(context: Context, parent: ViewGroup?) :
            AbsNavigationParams(context, parent) {
            // 所有效果放置
            var mTitle: String? = null
            var mRightText: String? = null
            var mRrightIcon: Int = 0
            var mRightClickListener: OnClickListener? = null
            var mLeftClickListener: OnClickListener = object : OnClickListener {
                override fun onClick(v: View?) {
                    (context as Activity).finish()
                }
            }
        }
    }
}